import {Component} from '@angular/core';
import {AdminListHttpService} from "../../service/http/admin.list.http.service";
import {HttpCustomConfig} from "../../service/base/base.http.service";
import {FormBuilder, FormControl, FormGroup} from '@angular/forms';
import {NzModalService} from "ng-zorro-antd/modal";
import {AdminDetailComponent} from "../../dialog/admin-detail/admin-detail.component";

@Component({
  selector: 'app-admin',
  templateUrl: './admin.component.html',
  styleUrls: ['./admin.component.less']
})

export class AdminComponent {
  dataList: any[] = [];
  isVisible: boolean = false;

  validateForm: FormGroup<{
    datePicker: FormControl<Date | null>;
    datePickerTime: FormControl<Date | null>;
    monthPicker: FormControl<Date | null>;
    rangePicker: FormControl<[Date, Date] | null>;
    rangePickerTime: FormControl<[Date, Date] | null>;
    timePicker: FormControl<Date | null>;
  }> = this.fb.group({
    datePicker: this.fb.control<Date | null>(null),
    datePickerTime: this.fb.control<Date | null>(null),
    monthPicker: this.fb.control<Date | null>(null),
    rangePicker: this.fb.control<[Date, Date] | null>(null),
    rangePickerTime: this.fb.control<[Date, Date] | null>(null),
    timePicker: this.fb.control<Date | null>(null)
  });

  constructor(private fb: FormBuilder,
              private modalService: NzModalService,
              private adminListHttpService: AdminListHttpService) {
  }

  defaultHttpConfig: HttpCustomConfig = {showLoading: false}

  ngOnInit() {
    this.queryData()
  }

  queryData() {
    this.adminListHttpService.post({}, this.defaultHttpConfig).subscribe((res) => {
      this.dataList = res.data.dataList
    })
  }

  submitData(data: any) {
    return this.adminListHttpService.addPost(data, this.defaultHttpConfig).subscribe((res) => {
      this.dataList = res.data.dataList
    })
  }

  handleOk(): void {
    console.log('Button ok clicked!');
    this.isVisible = false;
  }

  handleCancel(): void {
    console.log('Button cancel clicked!');
    this.isVisible = false;
  }

  switchOnline(data: any): void {
    // console.log(type,'zh type')
    //todo 调用接口
    data.online = !data.online;
  }

  submitForm(): void {
    console.log(this.validateForm.value);
  }

  checkSimplePhoneNum(value: any) {
    return /^1[0-9]{10}$/.test(value)
  }

  addData(): void {
    this.modalService.create({
      nzTitle: '添加',
      nzMaskClosable: false,
      nzContent: AdminDetailComponent,
      nzFooter: null,
      // nzOnOk:(res)=>{
      //   console.log(res,'res zh hhh')
      //   if (res.account==='' || res.adminName === '' || res.pwd === '') {
      //     this.message.create('error', `请填写所有信息`);
      //     return false
      //   }
      //   if (!this.checkSimplePhoneNum(res.account)) {
      //     this.message.create('error', `请输入正确的手机号`);
      //     return false
      //   }
      //   const params = {
      //     adminName:res.adminName,
      //     account:res.account,
      //     authType:res.authType,
      //     adminAuthType:res.adminAuthType,
      //     pwd:res.pwd
      //   }
      //   console.log(this.submitData(params),'zh this.submitData(params)')
      //   return
      // },
    }).afterClose.subscribe((): void => {
      console.log('zh 123')
      // this.queryData();
    });
  }

  editData(data: any): void {
    console.log(data, 'zh data')
    this.modalService.create({
      nzTitle: '编辑',
      nzMaskClosable: false,
      nzData: {data: data},
      // nzContent: AdminDetailComponent,
      nzContent: '111',
    }).afterClose.subscribe((): void => {
      this.queryData();
    });
  }

}

